
package com.insigma.business.qrcode.soldier._provider.impl;

import com.google.common.collect.Lists;

import com.insigma.business.qrcode.common._provider.BaseProviderImpl;
import com.insigma.business.qrcode.common.service.CommonAdminAreaService;
import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.LogUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dto.SoldierBSearcher;
import com.insigma.business.qrcode.soldier.dto.SoldierDTO;
import com.insigma.business.qrcode.soldier.entity.SoldierDO;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier.request.SoldierInsertRequest;
import com.insigma.business.qrcode.soldier.request.SoldierSearchRequest;
import com.insigma.business.qrcode.soldier.request.SoldierUpdateRequest;
import com.insigma.business.qrcode.soldier._provider.BaseSoldierProvider;
import com.insigma.business.qrcode.soldier.service.SoldierService;
import com.insigma.business.qrcode.common.converter.SoldierProviderConverter;
import com.insigma.business.qrcode.soldier.util.BizCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Component("baseSoldierProvider")
public class BaseSoldierProviderImpl extends BaseProviderImpl implements BaseSoldierProvider {

    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    @Autowired
    private SoldierService soldierService;

    @Autowired
    private CommonAdminAreaService commonAdminAreaService;

    @Override
    public BaseResponse<Integer> deleteSoldier(BaseDeleteRequest deleteRequest) {
        LogUtil.info(log, "逻辑删除Soldier，请求参数:", deleteRequest);
        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除Soldier的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) deleteRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final Integer affectRowNum = soldierService.delete((String) deleteRequest.getId(), deleteRequest.getModifierType(), (String) deleteRequest.getModifierId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_DELETE_LOGICALLY;
            }
        });
        return baseResponse;    }

    @Override
    public BaseResponse<Integer> updateSoldier(SoldierUpdateRequest updateRequest) {
        LogUtil.info(log, "修改Soldier，请求参数:", updateRequest);
        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改Soldier的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                SoldierDO soldierDO = SoldierProviderConverter.convertRequestUO2DO(updateRequest);
                Integer affectRowNum = soldierService.update(soldierDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_UPDATE;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> insertSoldier(SoldierInsertRequest insertRequest) {
        LogUtil.info(log, "新增Soldier，请求参数:", insertRequest);
        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增Soldier的请求参数不能为空");
            }

            @Override
            public void executeService() {
                insertRequest.setId(insertRequest.getAccountName());
                final SoldierDO soldierDO = SoldierProviderConverter.convertRequestIO2DO(insertRequest);
                Integer insert = soldierService.insert(soldierDO);
                ResponseUtils.setupSuccessResponse(baseResponse, insert);

            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_INSERT;
            }
        });
        return baseResponse;
    }

    @Override
    public BasePagingResponse<List<SoldierDTO>> searchSoldier(SoldierSearchRequest searchRequest) {
        LogUtil.info(log, "查询分页Soldier，请求参数:", searchRequest);
        final BasePagingResponse<List<SoldierDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页Soldier的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final SoldierBSearcher soldierBSearcher = SoldierProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<SoldierDO> soldierDOList = soldierService.search(soldierBSearcher);
                final List<SoldierDTO> soldierDTOList = SoldierProviderConverter.convertResponseDOList2DTOList(soldierDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, soldierDTOList, searchRequest, soldierBSearcher.getTotalCount());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_SEARCH;
            }
        });

        return basePagingResponse;    }

    @Override
    public BaseResponse<SoldierDTO> getSoldier(final BaseGetRequest baseGetRequest) {
        LogUtil.info(log, "获取单条Soldier，请求参数:", baseGetRequest);
        final BaseResponse<SoldierDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条Soldier的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final SoldierDO soldierDO = soldierService.get((String) baseGetRequest.getId());
                final SoldierDTO remoteObj = SoldierProviderConverter.convertResponseDO2DTO(soldierDO);
                ResponseUtils.setupSuccessResponse(baseResponse, remoteObj);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_GET;
            }
        });
        return baseResponse;
    }


    

    


    

    



    

    



    private Date yearToDate(String str) {
        if (Strings.isEmpty(str)) {
            return null;
        }
        DateFormat fmt = new SimpleDateFormat("yyyyMMdd");
        try {
            return fmt.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    private String dealWithType(String type) {
        String a = type.replace("和", ",");
        String[] split = a.split(",");
        ArrayList<String> objects = Lists.newArrayList();
        for (String s : split) {
            objects.add(s.trim());
        }
        String join = String.join(",", split);
        return join;
    }

    

}
